package com.shundr.route.http;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.shundr.frame.constant.LCommands;
import com.shundr.frame.http.HttpResult;
import com.shundr.frame.http.LHttpRequest;
import com.shundr.frame.http.LHttpRequest.RequestCompleteListener;
import com.shundr.route.model.TruckerRouteInfo;

public class RouteRequest extends LHttpRequest implements RequestCompleteListener{
	
	public static final int GET_ROUTE_LIST_SUCCESSED = 1;//获取常跑路线成功
	public static final int ADD_ROUTE_SUCCESSED = 2;//添加常跑路线成功
	public static final int DELETE_ROUTE_SUCCESSED = 3;//删除常跑路线成功
	public static final int DISABLE_ROUTE_SUCCESSED = 4;//关闭自动接单成功
	public static final int ENABLE_ROUTE_SUCCESSED = 5;//开启自动接单成功
	
	private Handler mHandler;
	private Message mMessage;
	private Context mContext;
	
	private Map<String, Object> param;
	private String command;
	
	public RouteRequest(Context context, Handler handler){
		this.mContext = context;
		this.mHandler = handler;
		this.mMessage = mHandler.obtainMessage();
		this.setRequestCompleteListener(this);
	}
	
	/**
	 * 获取常跑路线列表
	 * getRouteList(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void getRouteList(){
		param = new HashMap<String, Object>();
		command = LCommands.GET_ROUTE_LIST;
		requestByGet(param);
	}
	
	/**
	 * 添加线路
	 * addRoute(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param srcPlaceCode
	 * @param dstPlaceCode
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void addRoute(String srcPlaceCode,String dstPlaceCode){
		param = new HashMap<String, Object>();
		param.put("dstPlaceCode", dstPlaceCode);
		param.put("srcPlaceCode", srcPlaceCode);
		command = LCommands.ADD_ROUTE;
		requestByGet(param);
	}
	
	/**
	 * 删除路线
	 * @param routeId
	 */
	public void deleteRoute(String routeId){		
		param = new HashMap<String, Object>();
		param.put("routeId", routeId);
		command=LCommands.DELETE_ROUTE +"/"+routeId;
		requestByGet(param);
	}
	
	/**
	 * 关闭路线
	 * @param routeId
	 */
	public void disableRoute(){		
		param = new HashMap<String, Object>();
		command=LCommands.DISABLE_ROUTES;
		requestByGet(param);
	}
	
	/**
	 * 开启路线
	 * @param routeId
	 */
	public void enableRoute(){		
		param = new HashMap<String, Object>();
		command=LCommands.ENABLE_ROUTES;
		requestByGet(param);
	}
	
	@Override
	protected void initParams() {
		super.initParams();
		mParam = param;
		mCommand = command;
		mBaseContext = mContext;
	}

	@Override
	public void requestSuccessed(HttpResult result) {
		if(mHandler == null)
			return;
		if (command.equals(LCommands.GET_ROUTE_LIST)) {
			mMessage.what = GET_ROUTE_LIST_SUCCESSED;
			List<TruckerRouteInfo> infos = RouteParser.parserRouteList(result.getResult());
			mMessage.obj = infos;
			mHandler.sendMessage(mMessage);
		}else if (command.equals(LCommands.ADD_ROUTE)) {
			mMessage.what = ADD_ROUTE_SUCCESSED;;
			mMessage.obj = result.getDescription();
			mHandler.sendMessage(mMessage);
		}else if(command.contains(LCommands.DELETE_ROUTE)){
			mMessage.what = DELETE_ROUTE_SUCCESSED;;
			mMessage.obj = result.getDescription();
			mHandler.sendMessage(mMessage);
		}else if(command.equals(LCommands.DISABLE_ROUTES)){
			mMessage.what = DISABLE_ROUTE_SUCCESSED;
			mMessage.obj = result.getDescription();
			mHandler.sendMessage(mMessage);
		}else if(command.equals(LCommands.ENABLE_ROUTES)){
			mMessage.what = ENABLE_ROUTE_SUCCESSED;
			mMessage.obj = result.getDescription();
			mHandler.sendMessage(mMessage);
		}
	}

	@Override
	public void requestFailed(HttpResult result) {
		if(mHandler == null)
			return;
//		请求失败,result的返回码可判断失败原因，进行如下操作
		mMessage.what = REQUEST_FAILED;
		mMessage.obj = result.getDescription();
		mHandler.sendMessage(mMessage);
	}

}
